package com.mz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mz.common.dto.GlobalException;
import com.mz.common.dto.PaginationVo;
import com.mz.common.dto.R;
import com.mz.dao.UmsPermissionInfoDao;
import com.mz.entity.UmsPermissionInfo;
import com.mz.entity.vo.UmsPermissionInfoQuery;
import com.mz.service.UmsPermissionInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (UmsPermissionInfo)表服务实现类
 *
 * @author makejava
 * @since 2023-12-13 13:38:35
 */
@Service
public class UmsPermissionInfoServiceImpl  implements UmsPermissionInfoService {

    @Resource
    private UmsPermissionInfoDao umsPermissionInfoDao;

    /**
     * 获取分页
     * */
    @Override
    public IPage getPermissionPagination(PaginationVo paginationVo, UmsPermissionInfoQuery umsPermissionInfoQuery) throws Exception {

        //把第一级的数据进行分页查出来
        IPage page = new Page( paginationVo.getPageIndex(), paginationVo.getPageSize() );
        LambdaQueryWrapper<UmsPermissionInfo> lqs = new LambdaQueryWrapper<>();
        lqs.eq( UmsPermissionInfo::getLevel, 0 );
        umsPermissionInfoDao.selectPage( page, lqs );

        List<UmsPermissionInfo> list = page.getRecords();
        if( list.size() == 0 ){
            return page;
        }

        //获取第二级数据
        LambdaQueryWrapper<UmsPermissionInfo> secondLqs = new LambdaQueryWrapper<>();
        secondLqs.gt( UmsPermissionInfo::getLevel, 0 );
        List<UmsPermissionInfo> secondUmsPermissionInfos = umsPermissionInfoDao.selectList(secondLqs);
        if( secondUmsPermissionInfos == null || secondUmsPermissionInfos.size() == 0 ){
            return  page;
        }
        for( UmsPermissionInfo row: list ){
            List<UmsPermissionInfo> filterList = secondUmsPermissionInfos.stream().filter( item->{
               return item.getParentId().equals( row.getId() );
            }).collect(Collectors.toList());
            row.setChildren(filterList);

            System.out.println("filterList: "+filterList);

            for( UmsPermissionInfo subRow: filterList ){
                List<UmsPermissionInfo> subFilterList = secondUmsPermissionInfos.stream().filter( item->{
                    return item.getParentId().equals( subRow.getId() );
                }).collect(Collectors.toList());
                subRow.setChildren(subFilterList);
            }
        }
        return page;
    }





    /**
     * 新增权限项。
     * */
    @Override
    public void addPermission(UmsPermissionInfo umsPermissionInfo) throws Exception {

        umsPermissionInfo.setId(null);
        if( umsPermissionInfo.getParentId() == null ){
            umsPermissionInfo.setParentId(0L);
            umsPermissionInfo.setLevel(0);
        }
        LambdaQueryWrapper<UmsPermissionInfo> lqs = new LambdaQueryWrapper<>();
        lqs.eq(UmsPermissionInfo::getCode, umsPermissionInfo.getCode() );
        UmsPermissionInfo umsPermissionInfoInSQL = umsPermissionInfoDao.selectOne(lqs);
        if( umsPermissionInfoInSQL != null ){
            throw GlobalException.gen(10000, "权限code已存在");
        }
        umsPermissionInfoDao.insert(umsPermissionInfo);
    }

    @Override
    public void removePermission(List<Long> idList) throws Exception {
        if( idList == null || idList.size() == 0 ){
            throw GlobalException.gen(10011, "权限id不存在");
        }
        umsPermissionInfoDao.deleteBatchIds(idList);
    }

    @Override
    public List<UmsPermissionInfo> getPermissionAllList() throws Exception {

        //1、先查出所有的权限菜单
        List<UmsPermissionInfo> sysMenus = umsPermissionInfoDao.selectList(null);
        //2、然后组装成树形结构
        return sysMenus.stream().filter(item->{
            //相当于拿第一级的菜单
            return item.getParentId() == 0;
        }).map( item->{
            List<UmsPermissionInfo> children = flatMenuList(item, sysMenus);
            item.setChildren(children);
            return  item;
        }).collect(Collectors.toList());
    }


    private  List<UmsPermissionInfo> flatMenuList( UmsPermissionInfo parent, List<UmsPermissionInfo> list ){

        return list.stream().filter( item->{
            return item.getParentId().equals(parent.getId());
        }).map( item-> {
            List<UmsPermissionInfo> children =  flatMenuList( item, list );
            item.setChildren(children);
            return item;
        }).collect(Collectors.toList());
    }
}

